//
//  MKTransitionManager.swift
//  MKPhotoDetailDemo
//
//  Created by 劢克科技 on 2019/3/27.
//  Copyright © 2019 劢克科技. All rights reserved.
//

import UIKit

class MKTransitionManager: NSObject {
    var duration = 0.25
    var currentIndex = 0
    var imageDatas = [MKImageData]()
}

extension MKTransitionManager: UIViewControllerAnimatedTransitioning {
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return duration
    }
    
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        let container = transitionContext.containerView
        let fromController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)
        let toController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)
        let fromView = transitionContext.view(forKey: .from)
        let toView = transitionContext.view(forKey: .to)

        if toController!.isBeingPresented {
            container.addSubview(toView!)
            toView!.alpha = 0
            toView!.transform = CGAffineTransform.init(scaleX: 0, y: 0)
            UIView.animate(withDuration: duration, animations: {
                toView!.alpha = 1
                toView!.transform = .identity
            }) { (_) in
                transitionContext.completeTransition(true)
            }
        } else if fromController!.isBeingDismissed {
            UIView.animate(withDuration: duration, animations: {
                fromView!.alpha = 0
            }) { (_) in
                transitionContext.completeTransition(true)
            }
        }
    }
}
